<template>
  <!--  <ContentWrap>-->
  <!--    &lt;!&ndash; 搜索工作栏 &ndash;&gt;-->
  <!--    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true">-->
  <!--      <el-form-item :label="t('qualificationReviewScene.fields.isVote')" prop="isVote">-->
  <!--        <el-select-->
  <!--          v-model="queryParams.isVote"-->
  <!--          clearable-->
  <!--          :placeholder="t('qualificationReviewScene.fields.isVote')"-->
  <!--          style="width: 120px"-->
  <!--        >-->
  <!--          <el-option-->
  <!--            v-for="status in voteStatusOptions"-->
  <!--            :label="status.label"-->
  <!--            :value="status.value"-->
  <!--            :key="status.value"-->
  <!--          />-->
  <!--        </el-select>-->
  <!--      </el-form-item>-->
  <!--      <el-form-item>-->
  <!--        <el-button @click="handleQuery">-->
  <!--          <Icon icon="ep:search" class="mr-5px" />{{ t('common.search') }}-->
  <!--        </el-button>-->
  <!--        <el-button @click="resetQuery">-->
  <!--          <Icon icon="ep:refresh" class="mr-5px" />{{ t('common.reset') }}-->
  <!--        </el-button>-->
  <!--      </el-form-item>-->
  <!--    </el-form>-->
  <!--  </ContentWrap>-->

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iPickBatch-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('qualificationReviewScene.fields.nickName')"
        field="nickName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="50"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.dsgh')"
        field="dsgh"
        align="center"
        show-header-overflow
        show-overflow
        min-width="50"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.deptName')"
        field="deptName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.gender')"
        field="gender"
        align="center"
        show-header-overflow
        show-overflow
        min-width="50"
      >
        <template #default="{ row }">
          <span v-if="row.gender === '1'">男</span>
          <span v-if="row.gender === '2'">女</span>
        </template>
      </vxe-column>
      <vxe-column
        :title="t('qualificationReviewScene.fields.birthday')"
        field="birthday"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.researchGroup')"
        field="researchGroup"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.technicalPosition')"
        field="technicalPosition"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.enrollmentMajor')"
        field="enrollmentMajor"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.highestDegree')"
        field="highestDegree"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.researchDirection')"
        field="researchDirection"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.totalFunds')"
        field="totalFunds"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.totalStudents')"
        field="totalStudents"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
      />
      <vxe-column
        :title="t('qualificationReviewScene.fields.isVote')"
        field="isVote"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      >
        <template #default="{ row }">
          <el-tag disable-transitions v-if="row.isVote === 0" type="warning">
            {{ t('qualificationReviewScene.voteStatus.unVote') }}
          </el-tag>
          <el-tag disable-transitions v-if="row.isVote === 1" type="success">
            {{ t('qualificationReviewScene.voteStatus.agree') }}
          </el-tag>
          <el-tag disable-transitions v-if="row.isVote === 2" type="danger">
            {{ t('qualificationReviewScene.voteStatus.disagree') }}
          </el-tag>
        </template>
      </vxe-column>
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="130"
      >
        <template #default="{ row }">
          <el-button link type="primary" @click="handleDetail(row)">
            {{ t('qualificationReviewScene.operate.detail') }}
          </el-button>
          <el-button v-if="row.isVote === 0" link type="primary" @click="handleClick(row)">
            {{ t('qualificationReviewScene.operate.vote') }}
          </el-button>
          <el-button v-else link type="success">
            {{ t('qualificationReviewScene.operate.voted') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 投票弹窗 -->
  <QualificationReviewVote ref="qualificationReviewVoteRef" @voted="getList" />
  <!-- 申请信息详情弹窗 -->
  <QualificationReviewApplyDetail ref="qualificationReviewApplyDetailRef" />
</template>

<script setup lang="ts">
import type { VxeTablePropTypes, VxeToolbarInstance, VxeTableInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import {
  qualificationReviewApi,
  QualificationReviewSceneTableVO,
  QualificationReviewSceneVO
} from '@/api/recruit/qualificationReview'
import { BaseInfoVO } from '@/api/recruit/reviewApply/types'
import QualificationReviewVote from './components/QualificationReviewVote.vue'
import QualificationReviewApplyDetail from "@/views/recruit/components/QualificationReviewApplyDetail.vue";

/** 招生资格审查评审现场 申请列表 */
defineOptions({ name: 'QualificationReviewScene' })

const { t } = useI18n() // 国际化
const route = useRoute() // 路由

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()
const qualificationReviewVoteRef = ref<InstanceType<typeof QualificationReviewVote>>()
const qualificationReviewApplyDetailRef = ref<InstanceType<typeof QualificationReviewApplyDetail>>()

const loading = ref(true) // 列表的加载中
const list = ref<QualificationReviewSceneTableVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  batchId: undefined,
  isVote: undefined
})
const queryFormRef = ref() // 搜索的表单

// 投票状态选项
const voteStatusOptions = [
  { label: t('qualificationReviewScene.voteStatus.unVote'), value: 0 },
  { label: t('qualificationReviewScene.voteStatus.agree'), value: 1 },
  { label: t('qualificationReviewScene.voteStatus.disagree'), value: 2 }
]

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  queryParams.batchId = batchId.value as any
  loading.value = true
  try {
    const data = await qualificationReviewApi.getReviewBatchVoteList(queryParams)
    list.value = data.list.map((item) => {
      try {
        const baseInfo = JSON.parse(item.baseInformation) as BaseInfoVO
        return {
          ...item,
          ...baseInfo
        }
      } catch {
        return item
      }
    })
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  ;(queryParams as any).advanced = undefined
  handleQuery()
}

// 打开投票弹窗
const handleClick = (row: QualificationReviewSceneVO) => {
  qualificationReviewVoteRef.value?.open(row)
}

// 打开申请信息详情弹窗
const handleDetail = (row: QualificationReviewSceneVO) => {
  qualificationReviewApplyDetailRef.value?.open(row.detailId)
}

const batchId = ref('')
/** 初始化 **/
onMounted(() => {
  batchId.value = route.params.batchId as string
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>
